package com.hm.car.api;

import java.util.Date;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import com.hm.car.api.bean.OfferCompanyVo;
import com.hm.car.api.bean.OfferUserVo;
import com.hm.car.api.bean.OrderDetailsVo;
import com.hm.car.service.OfferService;
import com.hm.car.util.ControllerResult;
import com.hm.common.su.bean.PageInfo;
import com.hm.common.su.bean.ServerResponse;
import com.hm.common.util.StringUtil;

import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;

/**
 * @author shishun.wang
 * @date 2016年12月14日 上午10:15:28
 * @version 1.0
 * @describe
 */
@Slf4j
@ResponseBody
@RestController
@RequestMapping("/restful/api/offer")
@CrossOrigin(methods = { RequestMethod.GET, RequestMethod.OPTIONS, RequestMethod.POST, RequestMethod.PUT })
public class ApiOfferController {

	private static final String SWWAGER_TAG = "车保报价信息查询";

	private static final String OFFER_USER_SWWAGER_TAG = "车保用户基本信息";

	@Autowired
	private OfferService offerService;

	@RequestMapping(path = "/user/{carLicenseNo}", method = RequestMethod.GET)
	@ApiOperation(tags = OFFER_USER_SWWAGER_TAG, value = "加载车保用户信息", httpMethod = "GET", notes = "加载车保用户信息")
	public ResponseEntity<ServerResponse<OfferUserVo>> loadOfferUser(
			@ApiParam(required = true, name = "carLicenseNo", value = "用户车牌号") @PathVariable(name = "carLicenseNo", required = false) String carLicenseNo) {
		try {
			log.debug("加载车保用户信息,用户的车牌号是{}", carLicenseNo);
			if (StringUtil.isBlank(carLicenseNo.trim())) {
				return ControllerResult.failed("用户车牌号不能为空");
			}
			return ControllerResult.success(offerService.loadOfferUser(carLicenseNo));
		} catch (Exception e) {
			e.printStackTrace();
			return ControllerResult.failed(e);
		}
	}

	@RequestMapping(path = "/user/{carLicenseNo}", method = RequestMethod.POST)
	@ApiOperation(tags = OFFER_USER_SWWAGER_TAG, value = "添加车保用户信息", httpMethod = "POST", notes = "添加车保用户信息")
	public ResponseEntity<ServerResponse<Boolean>> saveOfferUser(
			@ApiParam(required = true, name = "carLicenseNo", value = "用户车牌号") @PathVariable("carLicenseNo") String carLicenseNo,
			@ApiParam(required = true, name = "body", value = "车保用户信息数据结构体") @RequestBody(required = true) OfferUserVo user) {
		try {
			log.debug("添加车保用户信息,用户的车牌号是{}", carLicenseNo);
			if (StringUtil.isBlank(carLicenseNo.trim())) {
				return ControllerResult.failed("用户车牌号不能为空");
			}
			log.debug(new Date()+"车主信息："+user);
			offerService.saveOfferUser(user, carLicenseNo);
			return ControllerResult.success(true);
		} catch (Exception e) {
			e.printStackTrace();
			return ControllerResult.failed(e);
		}
	}

	@RequestMapping(path = "/user/{carLicenseNo}", method = RequestMethod.PUT)
	@ApiOperation(tags = OFFER_USER_SWWAGER_TAG, value = "修改车保用户信息", httpMethod = "PUT", notes = "修改车保用户信息")
	public ResponseEntity<ServerResponse<Boolean>> updateOfferUser(
			@ApiParam(required = true, name = "carLicenseNo", value = "用户车牌号") @PathVariable("carLicenseNo") String carLicenseNo,
			@ApiParam(required = true, name = "body", value = "车保用户信息数据结构体") @RequestBody(required = true) OfferUserVo user) {
		try {
			log.debug("修改车保用户信息,用户的车牌号是{}", carLicenseNo);
			if (StringUtil.isBlank(carLicenseNo.trim())) {
				return ControllerResult.failed("用户车牌号不能为空");
			}
			offerService.updateOfferUser(user, carLicenseNo);
			return ControllerResult.success(true);
		} catch (Exception e) {
			e.printStackTrace();
			return ControllerResult.failed(e);
		}
	}

	@RequestMapping(path = "/query/{pageNumber}/{pageSize}/{addUser}", method = RequestMethod.GET)
	@ApiOperation(tags = SWWAGER_TAG, value = "分页查询订单", httpMethod = "GET", notes = "分页查询订单")
	public ResponseEntity<ServerResponse<PageInfo<OrderDetailsVo>>> queryOrderDetails(
			@ApiParam(required = true, name = "pageNumber", value = "分页号(第一页值为1,小于1则默认返回第一页)") @PathVariable("pageNumber") Integer pageNumber,
			@ApiParam(required = true, name = "pageSize", value = "分页大小") @PathVariable("pageSize") Integer pageSize,
			@ApiParam(required = true, name = "addUser", value = "添加人") @PathVariable("addUser") String addUser) {
		try {
			log.debug("分页查询订单,请求uri 是 /query/{}/{}/{}", pageNumber, pageSize,addUser);
			return ControllerResult.success(offerService.queryOrderDetails(pageNumber, pageSize,addUser));
		} catch (Exception e) {
			e.printStackTrace();
			return ControllerResult.failed(e);
		}
	}

	@RequestMapping(path = "/order/{taskId}", method = RequestMethod.GET)
	@ApiOperation(tags = SWWAGER_TAG, value = "查询单个订单详情", httpMethod = "GET", notes = "查询单个订单详情")
	public ResponseEntity<ServerResponse<OrderDetailsVo>> getOrderDetail(
			@ApiParam(required = true, name = "taskId", value = "订单ID") @PathVariable("taskId") String taskId) {
		try {
			log.debug("查询单个订单详情：taskId ：{}", taskId);
			return ControllerResult.success(offerService.getOrderDetail(taskId));
		} catch (Exception e) {
			e.printStackTrace();
			return ControllerResult.failed(e);
		}
	}

	@RequestMapping(path = "/company/{prvId}/{taskId}", method = RequestMethod.GET)
	@ApiOperation(tags = SWWAGER_TAG, value = "查询单个报价公司", httpMethod = "GET", notes = "查询单个报价公司")
	public ResponseEntity<ServerResponse<OfferCompanyVo>> getOfferCompany(
			@ApiParam(required = true, name = "prvId", value = "报价公司ID") @PathVariable("prvId") String prvId,
			@ApiParam(required = true, name = "taskId", value = "订单ID") @PathVariable("taskId") String taskId) {
		try {
			log.debug("查询单个报价公司：prvId ：{}", prvId);
			return ControllerResult.success(offerService.getOfferCompany(prvId, taskId));
		} catch (Exception e) {
			e.printStackTrace();
			return ControllerResult.failed(e);
		}
	}
	
	@RequestMapping(path = "/getUserByCondition/{pageNumber}/{pageSize}", method = RequestMethod.GET)
	@ApiOperation(tags = OFFER_USER_SWWAGER_TAG, value = "根据条件查询车保用户", httpMethod = "GET", notes = "根据条件查询车保用户")
	public ResponseEntity<ServerResponse<PageInfo>> getUserByCondition(
			@ApiParam(required = true, name = "pageNumber", value = "分页号(第一页值为1,小于1则默认返回第一页)") @PathVariable("pageNumber") Integer pageNumber,
			@ApiParam(required = true, name = "pageSize", value = "分页大小") @PathVariable("pageSize") Integer pageSize,
			@ApiParam(required = false, name = "addUser", value = "添加人") @RequestParam(name="addUser",required=false) String addUser,
			@ApiParam(required = false, name = "carLicenseNo", value = "车牌号码") @RequestParam(name="carLicenseNo",required=false) String carLicenseNo) {
		try {
			log.debug("根据条件查询车保用户,请求uri 是 /getUserByCondition/{}/{}/{}/{}", pageNumber, pageSize,addUser,carLicenseNo);
			return ControllerResult.success(offerService.getUserByCondition(pageNumber, pageSize,addUser,carLicenseNo));
		} catch (Exception e) {
			e.printStackTrace();
			return ControllerResult.failed(e);
		}
	}
	
	@RequestMapping(path = "/getOfferModelByCondition/{pageNumber}/{pageSize}", method = RequestMethod.GET)
	@ApiOperation(tags = SWWAGER_TAG, value = "根据条件查询保单信息", httpMethod = "GET", notes = "根据条件查询保单信息")
	public ResponseEntity<ServerResponse<PageInfo<OrderDetailsVo>>> getOfferModelByCondition(
			@ApiParam(required = true, name = "pageNumber", value = "分页号(第一页值为1,小于1则默认返回第一页)") @PathVariable("pageNumber") Integer pageNumber,
			@ApiParam(required = true, name = "pageSize", value = "分页大小") @PathVariable("pageSize") Integer pageSize,
			@ApiParam(required = false, name = "taskId", value = "任务号") @RequestParam(name="taskId",required=false) String taskId,
			@ApiParam(required = false, name = "carLicenseNo", value = "车牌号码") @RequestParam(name="carLicenseNo",required=false) String carLicenseNo,
			@ApiParam(required = false, name = "taskState", value = "任务状态") @RequestParam(name="taskState",required=false) String taskState,
			@ApiParam(required = false, name = "addUser", value = "添加人") @RequestParam(name="addUser",required=false) String addUser,
			@ApiParam(required = false, name = "begCreatTime", value = "创建的时间段开始") @RequestParam(name="begCreatTime",required=false) Long begCreatTime,
			@ApiParam(required = false, name = "endCreatTime", value = "创建的时间段结束") @RequestParam(name="endCreatTime",required=false) Long endCreatTime) {
		try {
			log.debug("根据条件查询保单信息,请求uri 是 /getOfferModelByCondition/{}/{}", pageNumber, pageSize);
			return ControllerResult.success(offerService.getOfferModelByCondition(pageNumber, pageSize,taskId,carLicenseNo,taskState,addUser,null,null));
		} catch (Exception e) {
			e.printStackTrace();
			return ControllerResult.failed(e);
		}
	}
}
